package top.xiangqifu.springboot2_auth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.xiangqifu.springboot2_auth.base.BasePageResult;
import top.xiangqifu.springboot2_auth.base.BaseResult;
import top.xiangqifu.springboot2_auth.dao.TbUserMessageDAO;
import top.xiangqifu.springboot2_auth.dao.TbUserMessageRelationshipDAO;
import top.xiangqifu.springboot2_auth.entity.AuthInfo;
import top.xiangqifu.springboot2_auth.entity.PageBean;
import top.xiangqifu.springboot2_auth.entity.TbUserMessage;
import top.xiangqifu.springboot2_auth.service.MessageService;
import top.xiangqifu.springboot2_auth.utils.AuthInfoUtils;
import top.xiangqifu.springboot2_auth.vo.MessageVo;

import java.util.List;
import java.util.Optional;


@Slf4j
@Service
@RequiredArgsConstructor
public class MessageServiceImpl implements MessageService {

    private final TbUserMessageDAO tbUserMessageDAO;

    private final TbUserMessageRelationshipDAO tbUserMessageRelationshipDAO;

    @Override
    public BasePageResult<List<TbUserMessage>> queryAll(MessageVo messageVo, PageBean pageBean) {
            TbUserMessage tbUserMessage=messageVo.getTbUserMessage();
            tbUserMessage = Optional.ofNullable(tbUserMessage).orElse(new TbUserMessage());
            if (StringUtils.hasText(tbUserMessage.getInfo())) {
                tbUserMessage.setInfo(String.format("%%%s%%", tbUserMessage.getInfo()));
            }
            //分页处理
            pageBean = Optional.ofNullable(pageBean).orElse(new PageBean());
            //启动分页处理
            PageHelper.startPage(pageBean.getPageNumber(), pageBean.getPageSize());
            if (messageVo.getOrderBy()==1){
                List<TbUserMessage> list = tbUserMessageDAO.queryAll1(tbUserMessage);
                BasePageResult result = getBasePageResult(pageBean, list);
                return result;
            }
            if (messageVo.getOrderBy()==2){
                List<TbUserMessage> list = tbUserMessageDAO.queryAll2(tbUserMessage);
                BasePageResult result = getBasePageResult(pageBean, list);
                return result;
            }
        if (messageVo.getOrderBy()==3){
            List<TbUserMessage> list = tbUserMessageDAO.queryAll3(tbUserMessage);
            BasePageResult result = getBasePageResult(pageBean, list);
            return result;
        }
        if (messageVo.getOrderBy()==4){
            List<TbUserMessage> list = tbUserMessageDAO.queryAll4(tbUserMessage);
            BasePageResult result = getBasePageResult(pageBean, list);
            return result;
        }
        if (messageVo.getOrderBy()==5){
            List<TbUserMessage> list = tbUserMessageDAO.queryAll5(tbUserMessage);
            BasePageResult result = getBasePageResult(pageBean, list);
            return result;
        }
        if (messageVo.getOrderBy()==6){
            List<TbUserMessage> list = tbUserMessageDAO.queryAll6(tbUserMessage);
            BasePageResult result = getBasePageResult(pageBean, list);
            return result;
        }
        if (messageVo.getOrderBy()==7){
            List<TbUserMessage> list = tbUserMessageDAO.queryAll7(tbUserMessage);
            BasePageResult result = getBasePageResult(pageBean, list);
            return result;
        }
            List<TbUserMessage> list = tbUserMessageDAO.queryAll8(tbUserMessage);
            BasePageResult result = getBasePageResult(pageBean, list);
            return result;
    }

    private static BasePageResult getBasePageResult(PageBean pageBean, List<TbUserMessage> list) {
        PageInfo<TbUserMessage> pageInfo = new PageInfo<TbUserMessage>(list);
        pageBean.setPageInfo(pageInfo);
        BasePageResult result = new BasePageResult();
        result.setSuccess(true);
        result.setCode(200);
        result.setMessage("");
        result.setPage(pageBean);
        result.setData(list);
        return result;
    }

    @Override
    public BaseResult add(TbUserMessage tbUserMessage) {
        AuthInfo authInfo= AuthInfoUtils.authInfo();
        if (authInfo.getUserId()==null&&authInfo.getUserId().equals("".toString())){
            return BaseResult.ok("请登录");
        }
        TbUserMessage tbUserMessage1=new TbUserMessage();
        tbUserMessage1.setUserId(authInfo.getUserId());
        tbUserMessage1.setInfo(tbUserMessage.getInfo());
        tbUserMessage1.setTitle(tbUserMessage.getTitle());
        log.debug("tbUserMessage:{}",tbUserMessage1);
        int i=tbUserMessageDAO.add(tbUserMessage1);
        log.debug("I:{}",i);
        if (i!=1){
            return BaseResult.ok("留言发布失败");
        }
        return BaseResult.ok("留言发布成功");

    }
}
